<?php

/*
function ThisPage()
{
    return $_SERVER['PHP_SELF'];
}
*/
function ActiveUrl()
{
    return $_SERVER['PHP_SELF'];
}

function h( $str )
{
    return htmlentities( $str, ENT_QUOTES ) ;
}

function UrlSelf()
{
    return $_SERVER['PHP_SELF'];
}

function self_url()
{
    return $_SERVER['PHP_SELF'];
}

function ReferringUrl()
{
    return $_SERVER['REQUEST_URI'];
}

function UserPmUrl($username)
{
    $id = UserID($username);
    return URL_INBOX."/privmsg.php?mode=post&u=$id";
}

function build_path($path, $filename)
{
    if(substr($filename, 0, 1) == '/') {
        $filename = substr($filename, 1, 1000);
    }

    if(substr($path, -1, 1) == '/') {
        $ret = $path . $filename;
    } else {
        $ret = $path . '/' . $filename;
    }
    return $ret;
}

function htmlEncode($value)
{
  return htmlspecialchars($value, ENT_QUOTES);
}

function htmlComment($str)
{
    return "<!--\n $str \n-->\n";
}

function permit_path($path)
{
    chmod($path, 0777);
}

function PrettySql( $sql )
{
    return str_replace( "\n", "<br />\n", $sql ) ;
}

function SqlEncode($str)
{
    return mysql_real_escape_string($str);
}

function SqlQuote($str)
{
    return "'$str'";
}

function HTTPRequest($val)
{
    return @$_REQUEST[$val];
}

function y_m_d()
{
    return date('Y-m-d');
}

function pretty_date_time($ts)
{
    if(empty($ts))
        return "--" ;
    else
    return strftime("%c", $ts) ;
}

function ProjectUrl($projectid)
{
    return URL_PROJECT."?projectid=$projectid";
}

function ProjectPageUrl($projectid, $pagecode)
{
	return ProjectUrl($projectid) . "/$pagecode/$pagecode.png";	
}

function ThumbFilePath($projectid, $pagecode)
{
        return ProjectPagePath($projectid, $pagecode)
        ."/".$this->ThumbFileName();
}

function ThumbUrl($projectid, $pagecode)
{
    return PROJECTS_URL
        ."/".$projectid
        ."/".$pagecode
        ."/".ThumbFileName($pagecode) ;
}

function ThumbFileName($pagecode)
{
    return "thumb.".$pagecode.".png";
}

function MaybeMakeFolder($path)
{
    if(! file_exists($path)) {
        mkdir($path, 0777);
    }
}

function ProjectPath($projectid)
{
    $path = build_path(PROJECTS_PATH, $projectid);
    MaybeMakeFolder($path);
    return $path;
}

function ProjectPagePath($projectid, $pagecode)
{
    $path = build_path(ProjectPath($projectid), $pagecode);
    MaybeMakeFolder($path);
	return $path;
}

function PageVersionPath($projectid, $pagecode, $version_number) {
    $p = ProjectPagePath($projectid, $pagecode);
    $v = "/v" .$version_number .".txt";
    return build_path($p, $v);
}

function debugDump( $thing )
{
    echo "<br />";
    var_dump( $thing ) ;
    echo "<br />";
}

function debugEcho( $msg )
{
    echo "<br />$msg<br />\n";
}

function error_box($errmsg)
{
    return <<<EOT
    <html>
    <head>
    <script language="Javascript [10]">
    <!--
        alert($errmsg);
        history.back();
    //-->
    </script>
    </head>
    <body></body>
    </html>
EOT;
}

function ErrorBox($errmsg)
{
    echo "
    <html>
    <head>
    <script language='Javascript [10]'>
    <!--
        alert($errmsg);
        history.back();
    //-->
    </script>
    </head>
    <body></body>
    </html>\n";
}

/*
function now()
{
    return new DpDate();
}

function today()
{
    return new DpDate();
}
*/

function Arg($code)
{
    if( isset( $_GET[$code] ) )
        return $_GET[$code] ;
    if( isset( $_POST[$code] ) )
        return $_POST[$code] ;
    if( isset( $_SESSION[$code] ) )
        return $_SESSION[$code] ;
    return "" ;
}

function endswith($big, $little)
{
    $ipos = strlen($little);
    return !strcasecmp(substr($big, -$ipos), $little);
}

function startswith($big, $little)
{
	return stripos($big, $little) === 0;
}

function starts_with($big, $little)
{
    return startswith($big, $little);
}

 function QuoteOrNull($code)
{
    return empty($code) ? "NULL" : "'$code'" ;
}

?>
